import api from '../../api';
import { message } from 'antd';
export default {
  namespace: 'portfolioTrackingsM',

  state: {},

  effects: {
    //获取产品组
    *getProductData({ payload, callback }, { call, put }) {
      const res = yield call(api.portfolioTracking.getProductData, payload);
      if (res && res.code == 0) {
        callback(res.data);
      } else {
        message.error(res.msg);
      }
    },

    //查询产品列表
    *queryProuctList({ payload, callback }, { call, put, select }) {
      const res = yield call(api.portfolioTracking.queryProuctList, payload);
      if (res && res.code == 0) {
        callback(res.data);
      } else {
        callback();
        message.error(res ? res.msg || '查询产品列表失败!' : '查询产品列表失败!');
      }
    },

    //获取产品下组合数据
    *getGroupList({ payload, callback }, { call, put, select }) {
      const res = yield call(api.getProductGroup, payload);
      console.log(res);
      if (res && res.success) {
        if (res.data.length > 0) {
          callback(res.data);
        } else {
          message.error('组合维度数据查询失败');
        }
      } else {
        message.error('查询失败1');
      }
    },

    //获取表头
    *getColumn({ payload, callback }, { call, put }) {
      let columns1 = [
        { dataIndex: 'portfolioName', title: `${payload.isSimulate == undefined ? '组合名称' : '产品名称'}`, key: 'portfolioName' },
        { dataIndex: 'assetType', title: '资产类别', key: 'assetType' },
        { dataIndex: 'TRDATTR', title: '交易属性', key: 'TRDATTR' },
        { dataIndex: 'POSTYPE', title: '持仓类型', key: 'POSTYPE' },
        { dataIndex: 'stockCode', title: '证券代码', key: 'stockCode' },
        { dataIndex: 'stockName', title: '证券简称', key: 'stockName' },
        { dataIndex: 'exchangecode', title: '交易市场', key: 'exchangecode' },
        { dataIndex: 'costPrice', title: '成本价', key: 'costPrice' },
        { dataIndex: 'newPrice', title: '最新价', key: 'newPrice' },
        { dataIndex: 'return', title: '涨跌幅(%)', key: 'return' },
        { dataIndex: 'quantity', title: '持仓数量', key: 'quantity' },
        { dataIndex: 'totalValue', title: '总市值', key: 'totalValue' },
        { dataIndex: 'weight', title: '权重(%)', key: 'weight' },
        { dataIndex: 'todayPnl', title: '当日盈亏', key: 'todayPnl' },
        { dataIndex: 'totalPnl', title: '总盈亏', key: 'totalPnl' },
        { dataIndex: 'industry', title: '所属行业', key: 'industry' },
      ];
      let columns2 = [
        { dataIndex: 'portfolioName', title: '产品名称', key: 'portfolioName' },
        { dataIndex: 'assetType', title: '资产类别', key: 'assetType' },
        { dataIndex: 'TRDATTR', title: '交易属性', key: 'TRDATTR' },
        { dataIndex: 'POSTYPE', title: '持仓类型', key: 'POSTYPE' },
        { dataIndex: 'stockCode', title: '证券代码', key: 'stockCode' },
        { dataIndex: 'stockName', title: '证券简称', key: 'stockName' },
        { dataIndex: 'exchangecode', title: '交易市场', key: 'exchangecode' },
        { dataIndex: 'newPrice', title: '最新价', key: 'newPrice' },
        { dataIndex: 'return', title: '涨跌幅(%)', key: 'return' },
        { dataIndex: 'weight', title: '权重(%)', key: 'weight' },
        { dataIndex: 'weightdiff', title: '对比产品权重差(%)', key: 'weightdiff' },
        { dataIndex: 'industry', title: '所属行业', key: 'industry' },
      ];

      const res = {
        columns: payload.isSimulate == 1 ? columns2 : columns1,
      };
      callback(res);
    },

    //获取表格树形数据
    *getRowInfo({ payload, callback }, { call, put }) {
      const res = yield call(api.portfolioTracking.getRowInfo, payload);
      if (res && res.code == 0) {
        callback(res.data);
      } else {
        message.error(res ? res.msg || '查询失败!' : '查询失败!');
      }
    },

    //获取指标数据
    *getTargetInfo({ payload, callback }, { call, put }) {
      const res = yield call(api.portfolioTracking.getTargetInfo, payload);
      if (res && res.code == 0) {
        callback(res.data);
      } else {
        message.error(res ? res.msg || '查询失败!' : '查询失败!');
      }
    },

    //获取区间收益率
    *getIncomeTable({ payload, callback }, { call, put }) {
      const res = yield call(api.portfolioTracking.getIncomeTable, payload);
      if (res && res.code == 0) {
        callback(res.data);
      } else {
        message.error(res ? res.msg || '查询失败!' : '查询失败!');
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
